<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrappe">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="单据来源">
              <j-dict-select-tag placeholder="请选择单据来源" @input="handleDocumentSource" v-model="queryParam.permissionType" dictCode="permission_type"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="queryParam.permissionType == 3">
            <a-form-item label="选择部门">
              <J-select-multiple
                placeholder="请选择选择部门"
                :options="getDepartName"
                showId="id"
                v-model="queryParam.deptIds"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="queryParam.permissionType == 4">
            <a-form-item label="选择团队">
              <J-select-multiple
                placeholder="请选择选择团队"
                :options="getTeamName"
                showId="id"
                v-model="queryParam.teamIds"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="盘点人">
              <select-user-by-dep
                v-model="queryParam.employeeId"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="盘点时间">
              <a-range-picker
                :allowClear="false"
                v-model="checkDate"
                style="width: 100%"
                @change="checkDateChange"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="checkDate=[],searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button @click="handleAdd" type="primary" icon="plus" :disabled="isDisabledAuth('erpPurchaseWarehouseCheckBill:add')">新增</a-button> -->
      <a-button @click="handleCheck" type="primary" icon="plus" :disabled="isDisabledAuth('erpPurchaseWarehouseCheckBill:add')">盘点</a-button>
      <!-- 高级查询区域 -->
      <!--<j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" :disabled="isDisabledAuth('erpPurchaseWarehouseCheckBill:delete')"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12" :key="index"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style="iconStyle"/></a>
      </a-popover>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{getCheckboxProps:getCheckboxProps,selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <span slot="notify" slot-scope="text, record">
          <a-icon
            title="催办提醒"
            v-if="record.taskUrge"
            theme="twoTone"
            twoToneColor="#eb2f96"
            @click="taskNotifyMe(flowCode,record.id)"
            type="notification"/>
          {{ text }}
        </span>
        <template slot="status" slot-scope="status">
          <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="detail" slot-scope="text, record">
          <a @click="detail(record)">仓库明细</a>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-button type="primary" size="small" @click="detail(record)" :disabled="isDisabledAuth('erpPurchaseWarehouseCheckBill:detail')">仓库明细</a-button>
          <a-divider type="vertical"/>
          <template v-if="record.bpmStatus === '1'">
            <!-- <a-button
                    class="ruleButton"
                    :ghost="true"
                    type="primary"
                    icon="edit"
                    @click="handleEdit(record)"
                    :disabled="isDisabledAuth('erpPurchaseWarehouseCheckBill:edit')"
            />
            <a-divider type="vertical"/> -->
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
              <a-button
                class="ruleButton"
                :ghost="true"
                type="primary"
                icon="delete"
                :disabled="isDisabledAuth('erpPurchaseWarehouseCheckBill:delete')"/>
            </a-popconfirm>
            <a-divider type="vertical"/>
            <a-button type="primary" size="small" @click="beginProcess(record)" :disabled="isDisabledAuth('erpPurchaseWarehouseCheckBill:startProcess')">发起流程</a-button>
          </template>
          <template v-else-if="showBtn(record.bpmStatus)&&queryParam.bizTaskType=='1'">
            <a-button type="primary" size="small" @click="handleProcess(record)">办理</a-button>
            <a-divider type="vertical"/>
          </template>
          <template v-else-if="queryParam.bizTaskType=='2' && showBtn(record.bpmStatus)">
            <a-button type="primary" size="small" @click="taskNotify(flowCode,record.id)">催办</a-button>
            <a-divider type="vertical"/>
            <a-popconfirm title="确定要作废吗?" @confirm="() => invalidProcess(record)">
              <a-button type="primary" size="small" :disabled="isDisabledAuth('erpPurchaseWarehouseCheckBill:invalidProcess')">
                作废流程
              </a-button>
            </a-popconfirm>
            <a-divider type="vertical"/>
          </template>
          <template v-if="record.bpmStatus !== '1'">
            <a-button type="primary" size="small" @click="handleTrack(record)" :disabled="isDisabledAuth('erpPurchaseWarehouseCheckBill:viewTrack')">审批进度</a-button>
          </template>
        </span>

      </a-table>
    </div>
    <ErpPurchaseWarehouseCheckList ref="warehouseCheck"></ErpPurchaseWarehouseCheckList>
    <erp-purchase-warehouse-check-bill-modal ref="modalForm" @ok="modalFormOk"></erp-purchase-warehouse-check-bill-modal>
    <bpm-biz-task-deal-modal ref="taskDealModal" :path="path" :formData="formData" @ok="handleOk"></bpm-biz-task-deal-modal>
    <bpm-process-track-modal ref="bpmProcessTrackModal"></bpm-process-track-modal>
    <biz-task-notify-modal ref="taskNotifyModal"></biz-task-notify-modal>
    <biz-task-notify-me-modal ref="taskNotifyMeModal"></biz-task-notify-me-modal>
    <process-config-modal ref="processConfig" @ok="handleSuccess"></process-config-modal>
    <selectStoreInputModal ref="inputModal" :multi="false" isUse="1" @choseObj="choseObj"></selectStoreInputModal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgBpmListMixin } from '@/mixins/JeecgBpmListMixin'
  import BpmBizTaskDealModal from '@/views/modules/bpmbiz/common/BpmBizTaskDealModal.vue';
  import BpmProcessTrackModal from '@/views/modules/bpmbiz/common/BpmProcessTrackModal.vue';
  import BizTaskNotifyModal from '@/views/modules/bpmbiz/common/BizTaskNotifyModal.vue';
  import BizTaskNotifyMeModal from '@/views/modules/bpmbiz/common/BizTaskNotifyMeModal.vue';
  import ErpPurchaseWarehouseCheckBillModal from './modules/ErpPurchaseWarehouseCheckBillModal'
  import selectStoreInputModal from './modules/reference/selectStoreInputModal' // 仓库弹窗
  import { getAction, postAction } from '@/api/manage'
  import { mapGetters } from 'vuex';
  import moment from 'moment'
  import { DisabledAuthFilterMixin } from '@/mixins/DisabledAuthFilterMixin'
  import ErpPurchaseWarehouseCheckList from './ErpPurchaseWarehouseCheckList.vue'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import ProcessConfigModal from '@/views/scy/components/ProcessConfigModal'

  export default {
    name: 'ErpPurchaseWarehouseCheckBillList',
    mixins: [JeecgBpmListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      ErpPurchaseWarehouseCheckBillModal,
      selectStoreInputModal,
      BpmBizTaskDealModal,
      BpmProcessTrackModal,
      BizTaskNotifyModal,
      BizTaskNotifyMeModal,
      ErpPurchaseWarehouseCheckList,
      SelectUserByDep,
      ProcessConfigModal
    },
    data () {
      return {
        billId: '',
        checkDate: [],
        description: '仓库盘点主表管理页面',
        // 表头
        columns: [],
        // 列设置
        settingColumns: [],
        // 列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '单据编号',
            align: 'center',
            width: 200,
            dataIndex: 'billNo'
          },
          {
            title: '盘点人',
            align: 'center',
            width: 200,
            dataIndex: 'employeeId_dictText'
          },
          {
            title: '所属部门',
            align: 'center',
            width: 200,
            dataIndex: 'departmentId_dictText'
          },
          {
            title: '盘点时间',
            align: 'center',
            width: 200,
            dataIndex: 'checkDate',
            customRender: function (text) {
              return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
            }
          },
          {
            title: '调整状态',
            align: 'center',
            width: 200,
            dataIndex: 'adjustType_dictText'
          },
          {
            title: '调整时间',
            align: 'center',
            width: 200,
            dataIndex: 'adjustmentDate'
          },
          {
            title: '审核状态',
            align: 'center',
            width: 200,
            dataIndex: 'bpmStatus_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            width: 147,
            scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action' }
          }
        ],
        url: {
          checkData: '/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/checkData',
          stocktakingWareHouse: '/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/stocktakingWareHouse',
          list: '/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/list',
          delete: '/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/delete',
          deleteBatch: '/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/deleteBatch',
          exportXlsUrl: '/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/exportXls',
          importExcelUrl: 'api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/importExcel',
          bizCount: '/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/bizCount'
        },
        // 代码生成后需手动设置流程编码
        flowCode: 'onl_erp_purchase_warehouse_check_bill',
        formUrl: 'erp/purchase/storehouse/modules/ErpPurchaseWarehouseCheckBillForm',
        formUrlMobile: '/pages/synergy/erpPurchaseWarehouseCheckBillList',
        dictOptions: {},
        superFieldList: [],
        processId: '1560190674218823682',
        beanName: 'erpPurchaseWarehouseCheckBillServiceImpl'
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      // 流程审批结束禁用
      getCheckboxProps(record) {
        return {
          props: {
            disabled: record.bpmStatus !== '1'
          }
        }
      },
      handleSuccess() {
        this.loadData();
        this.$refs.modalForm.handleCancel()
        this.onClearSelected();
      },
      // 发起流程
      beginProcess: function (record) {
        var that = this;
        getAction(this.url.checkData, { id: record.id }).then(res => {
          if (res.success) {
              this.$confirm({
              title: '提示',
              content: '确认提交流程吗?',
              onOk: function () {
                that.$refs.processConfig.visible = true
                that.$refs.processConfig.formUrl = that.formUrl
                that.$refs.processConfig.formUrlMobile = that.formUrlMobile
                that.$refs.processConfig.flowCode = that.flowCode
                that.$refs.processConfig.edit(record, '', that.beanName);
              }
            });
          } else {
             this.$message.warning(res.message);
          }
        })
      },
      // 仓库明细
      detail(record) {
        this.$refs.warehouseCheck.show(record.id, record.bpmStatus);
      },
      checkDateChange(date, dateString) {
        this.$set(this.queryParam, 'checkDate_begin', dateString[0] + ' 00:00:00')
        this.$set(this.queryParam, 'checkDate_end', dateString[1] + ' 23:59:59')
      },
      ...mapGetters(['userInfo']),

      choseObj (list) {
        // this.selectedObj = list;
        var ids = [];
        for (var i = 0; i < list.length; i++) {
          ids.push(list[i].id);
        }
        var objs=ids.join(",");
        if (!ids || ids.length == 0) {
          this.$message.warning('请选择要盘点的仓库！')
          return;
        }
        let userinfo = this.userInfo();
        getAction(this.url.stocktakingWareHouse, { employeeId: userinfo.empId, wareHouseId: objs, userName: userinfo.empName }).then(res => {
          if (res.success) {
            this.$message.success(res.message);
            this.loadData();
          } else {
            this.$message.warning(res.message);
          }
        })
      },
      // 盘点
      handleCheck() {
        this.$refs.inputModal.add([], []);
        
      },
      initDictConfig() {
      },
      getSuperFieldList() {
        let fieldList = [];
        fieldList.push({ type: 'int', value: 'checkStatus', text: '审核状态', dictCode: '' })
        fieldList.push({ type: 'string', value: 'employeeId', text: '盘点人', dictCode: '' })
        fieldList.push({ type: 'date', value: 'checkDate', text: '盘点时间' })
        fieldList.push({ type: 'string', value: 'storehouseId', text: '仓库', dictCode: '' })
        fieldList.push({ type: 'string', value: 'bpmStatus', text: '审核状态', dictCode: '' })
        fieldList.push({ type: 'string', value: 'adjustType', text: '调整状态', dictCode: '' })
        fieldList.push({ type: 'date', value: 'adjustmentDate', text: '调整时间' })
        fieldList.push({ type: 'string', value: 'departmentId', text: '组织机构', dictCode: '' })
        fieldList.push({ type: 'string', value: 'orgDepart', text: '所属单位', dictCode: '' })
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
